<template>
  <div class="flex justify-between text-[#000] text-lg pb-10">
    <div class="flex items-center cursor-pointer">
      <el-icon>
        <ArrowLeftBold />
      </el-icon>
      <p class="pl-2" @click="emits('handleReturn')">{{ $t("忘记密码") }}</p>
    </div>
  </div>
  <div>
    <el-form
      ref="formRef"
      :model="form"
      status-icon
      :rules="rules"
      label-position="top"
    >
      <el-form-item prop="email" :label="$t('邮箱')">
        <el-input
          v-model="form.email"
          size="large"
          :placeholder="$t('请输入邮箱地址')"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="code" :label="$t('验证码')">
        <div class="flex items-center justify-between flex-1">
          <el-input
            v-model="form.code"
            :placeholder="$t('请输入验证码')"
            size="large"
          >
          </el-input>

          <el-button
            size="large"
            class="ml-2 codeBtn"
            :plain="true"
            @click="handleCaptcha"
            :disabled="form.email === ''"
            v-if="!codesms.disabled"
          >
            {{ $t("获取验证码") }}
          </el-button>
          <el-button size="large" class="ml-2" disabled v-else :plain="true">
            {{ codesms.count }}{{ $t("秒后重新获取") }}
          </el-button>
        </div>
      </el-form-item>
      <el-form-item prop="password" :label="$t('新密码')">
        <el-input
          type="password"
          v-model="form.password"
          show-password
          size="large"
          class="mt-2"
          :placeholder="$t('请设置新密码')"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="again_password" :label="$t('确认密码')">
        <el-input
          type="password"
          v-model="form.again_password"
          show-password
          size="large"
          class="mt-2"
          :placeholder="$t('请再次输入新密码')"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <el-button
      size="large"
      :plain="true"
      class="w-full mt-2 loginBtn"
      :loading="loading"
      @click="onSubmit"
    >
      {{ $t("确定") }}
    </el-button>
  </div>
</template>
<script setup>
const emits = defineEmits(["handleReturn"]);
import { reactive, ref, computed } from "vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();

import { resetNewPasswordApi, emailSendApi } from "@/api/login/index";
import { ElMessage } from "element-plus";

const loading = ref(false);
const formRef = ref();

const codesms = reactive({
  disabled: false,
  total: 60,
  count: 0,
});

const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error(t("请再次输入新密码")));
  } else if (value !== form.password) {
    callback(new Error(t("密码不一致")));
  } else {
    callback();
  }
};
// 校验
const rules = computed(() => {
  return {
    email: [{ required: true, message: t("请输入邮箱地址"), trigger: "blur" }],
    password: [{ required: true, message: t("请输入新密码"), trigger: "blur" }],
    code: [{ required: true, message: t("请输入验证码"), trigger: "blur" }],
    again_password: [
      { required: true, validator: validatePass2, trigger: "blur" },
    ],
  };
});

const form = reactive({
  email: "",
  password: "",
  again_password: "",
  code: "",
});
// 倒计时
const timerHandle = () => {
  codesms.count = codesms.total;
  codesms.disabled = true;

  let timer = setInterval(() => {
    if (codesms.count > 1 && codesms.count <= codesms.total) {
      codesms.count--;
    } else {
      codesms.disabled = false;
      clearInterval(timer);
    }
  }, 1000);
};

//发送验证码
const handleCaptcha = async () => {
  try {
    timerHandle();
    const res = await emailSendApi({
      email: form.email,
      code_type: "forgot_password",
    });
    ElMessage({
      type: "success",
      message: t("发送验证码成功"),
    });
  } catch (error) {}
};
//提交
const onSubmit = () => {
  formRef.value
    .validate()
    .then(async () => {
      try {
        loading.value = true;
        // 1.调用忘记密码接口
        // console.log(form);
        const res = await resetNewPasswordApi(form);
        ElMessage({
          message: t("修改密码成功"),
          type: "success",
        });
        emits("handleReturn");
      } catch (error) {
        loading.value = false;
      }
    })
    .catch((error) => {
      // console.log(error);
    });
};
</script>
<style lang="scss" scoped>
.codeBtn {
  background-color: rgba(40, 61, 88, 1);
  color: #fff;
  border-radius: 10px;
}
</style>
